package com.biglead.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biglead.demo.common.PageRequest;
import com.biglead.demo.common.PageResult;
import com.biglead.demo.common.PageUtils;
import com.biglead.demo.mapper.UserMapper;
import com.biglead.demo.pojo.UserInfo;
import com.biglead.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper,UserInfo> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public List<UserInfo> findList() {
        log.info("查询所有的用户 调用  mybatis 方法");
        return userMapper.cuselectList();
    }
    @Override
    public UserInfo findById(Long userId) {
        log.info("查询指定用户 mybatis plus 方法 {}",userId);
        return userMapper.selectById(userId);
    }
    @Override
    public UserInfo newUser(UserInfo info) {
        //保存用户
        this.save(info);
        return info;
    }
    @Override
    public UserInfo updateUser(UserInfo info) {
        //根据ID来更新用户信息
        this.updateById(info);
        return info;
    }
    @Override
    public UserInfo delete(UserInfo info) {
        //根据ID来删除用户信息
        this.delete(info);
        return info;
    }
    @Override
    public PageResult findPage(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    @Override
    public UserInfo updateUserAge(UserInfo userInfo) {
        userMapper.updateUserInfo(userInfo.getUserId(),userInfo.getUserAge());
        return null;
    }

    /**
     * 调用分页插件完成分页
     * @return
     */
    private PageInfo<UserInfo> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<UserInfo> userInfoList = userMapper.selectPage();
        log.info("分页查询所有的用户 调用  mybatis 方法");
        return new PageInfo<UserInfo>(userInfoList);
    }

}
